package com.henry.server;

import com.henry.model.Question;
import com.henry.model.User;
import com.henry.model.UserMessage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.henry.server.Data.outputGrades;
import static com.henry.server.Data.users;

public class ServerDao {
    public boolean serverManagerCheck(User user) {
        if("admin".equals(user.getUserName()) && "123456".equals(user.getPassWord())) {
            return true;
        }
        return false;
    }
    public boolean serverStudentCheck(User user) {
        for (User us : users) {
            if(us.getUserName().equals(user.getUserName())&&us.getPassWord().equals(user.getPassWord())) {
                return true;
            }
        }
        return false;
    }
    public boolean addStudentCheck(User user) {
        for(User us : users) {
            if(us.getUserName().equals(user.getUserName())) {
                return false;
            }
        }
        users.add(user);
        return true;
    }
    public boolean deleteStudentCheck(UserMessage userMessage) {
        if(userMessage.getType().equals("true")) {
            return users.remove(userMessage.getUser());
        }
        return false;
    }
    public boolean reviseStudentCheck(User user) {
        for (User us : users) {
            if(us.getUserName().equals(user.getUserName())) {
                users.set(users.indexOf(us), new User(us.getUserName(),
                        user.getPassWord().equals("-1") ? us.getPassWord() : user.getPassWord(),
                        user.getName().equals("-1") ? us.getName() : user.getName(),
                        user.getGender().equals("-1") ? us.getGender() : user.getGender(),
                        user.getAge().equals("-1") ? us.getAge() : user.getAge(),
                        user.getCla().equals("-1") ? us.getCla() : user.getCla(),
                        user.getStatus().equals("-1") ? us.getStatus() : user.getStatus()));
                return true;
            }
        }
        return false;
    }
    public UserMessage findStudent(UserMessage userMessage) {
        for(User us : users) {
            if(us.getUserName().equals(userMessage.getUser().getUserName())) {
                return new UserMessage("true", us);
            }
        }
        return new UserMessage("false",new User());
    }

    public boolean revisePassword(User user) {
        for(User us : users) {
            if(us.getUserName().equals(user.getUserName())) {
                us.setPassWord(user.getPassWord());
            }
        }
        return true;
    }

    public UserMessage getQuestion() {
        return new UserMessage("getQuestion",Data.getQuestions());
    }

    public void postGrade(UserMessage userMessage) {
        User user = userMessage.getUser();
        for (User us : users) {
            if(us.getUserName().equals(user.getUserName())) {
                Data.putGrade(us.getUserName(), us.getName(), us.getCla(), user.getGrade());
            }
        }
    }

    public void outputGrade() throws IOException {
        outputGrades("e:/grades.txt");
    }
}
